import React, { Component } from 'react'
import { Table, Tag, Space,Button,Pagination, Popconfirm ,message } from 'antd';
import {getAdminsList,deleteOneAdmin} from '../../apis/adminsApi'
  
export default class AccountList extends Component {
    state={
        list:[],
        loading:false,
        total:0,
        pageSize:"5",
        curren:1
    }
    componentDidMount(){
       this.getList()
    }
    getList=async(params)=>{
       try {
        this.setState({loading:true})
        const data = await getAdminsList(params)
        console.log(data);
        this.setState({loading:false,list:data.data,total:data.total})
       } catch (error) {
           console.log('error',error);
       }
     }
     onChange=(pageNumber,pageSize)=>{
         console.log(pageNumber,pageSize);
         this.getList({pageNumber,pageSize})
     }
     deleteAdmin = async(id)=>{
         try {
             await deleteOneAdmin(id) 
             this.getList()
             message.success("删除成功")
         } catch (error) {
             console.log('error',error);
             message.error("删除失败")
         }
     }
    gotoUpdate=(_id)=>{
        this.props.history.push(`/main/updateAdmin?id=${_id}`)
    }
    render() {
        const columns =[
            {
              title: '账户名',
              dataIndex:"username",
              key:''
            },
            {
                title: '密码',
                dataIndex:"password",
              },
            {
                title: '权限',
                // dataIndex:"role"
                render:(item)=>{
                return <Button disabled>{item.role==1?"超级管理员":"管理员"}</Button>
                }
            },
            {
                title: '创建时间',
                dataIndex:"time"
            },
            {
                title: '操作',
                render:(item)=>{
                    return (
                        <div>
                    <Popconfirm
                    title="您确定要删除吗"
                    onConfirm={()=>this.deleteAdmin(item._id)}
                    // onCancel={cancel}
                    okText = "确定"
                    cancelText="取消"
                    >
                         <Button type="primary" style={{marginRight:"15px"}}>删除</Button>
                    </Popconfirm>
                    <Popconfirm
                    title="您确定要修改吗"
                    onConfirm={()=>this.gotoUpdate(item._id)}
                    // onCancel={cancel}
                    okText = "确定"
                    cancelText="取消"
                    >
                         <Button type="primary">修改</Button>
                         
                    </Popconfirm>
                    </div>
                    )
                    
                },
            },
        ]
        const {list,loading,total,pageSize} = this.state
        const pagination ={
            total,
            pageSize,
            onChange:this.onChange
        }
        return (
            <div style={{height:'100%'}}>
               <Table columns={columns} rowKey="_id" pagination={pagination} dataSource={list} loading={loading} />
            </div>
        )
    }
}
